package com.thoughtworks.biblioteca;

import java.lang.reflect.InvocationTargetException;

/**
 * A Factory Class. It is used to create a MenuItem class instance by the
 * reflection method.
 */
public class MenuItemFactory {
    @SuppressWarnings("unchecked")
    public static MenuItem getMenuItemInstance(String className, String itemName) {
        Class<MenuItem> menuItemClass = null;
        try {
            menuItemClass = (Class<MenuItem>) Class.forName(className);
            return menuItemClass.getConstructor(new Class[]{String.class})
                    .newInstance(new Object[]{itemName});

        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        } catch (InstantiationException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        } catch (NoSuchMethodException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        } catch (InvocationTargetException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

}
